function reply(str)
{

//获取表单对象和用户信息值
var inputId = "textReplyBlog"+str;
var content = document.getElementById(inputId).value;


//接收表单的URL地址
var url = "/tpapp/Xx/reply";

//需要POST的值，把每个变量都通过&来联接
var postStr= "bid="+str+"&content="+content;


var ajax = false;
//开始初始化XMLHttpRequest对象
if(window.XMLHttpRequest) { //Mozilla 浏览器
ajax = new XMLHttpRequest();
if (ajax.overrideMimeType) {//设置MiME类别
ajax.overrideMimeType("text/xml");
}
}
else if (window.ActiveXObject) { // IE浏览器
try {
ajax = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
ajax = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!ajax) { // 异常，创建对象实例失败
window.alert("不能创建XMLHttpRequest对象实例.");
return false;
}        

//通过Post方式打开连接
ajax.open("POST", url, true);

//定义传输的文件HTTP头信息
ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");

//发送POST数据
ajax.send(postStr);

//获取执行状态
ajax.onreadystatechange = function() {
//如果执行状态成功，那么就把返回信息输出
if (ajax.readyState == 4 && ajax.status == 200) {
	if(ajax.responseText.Trim()=="评论失败")
	{
		alert("评论失败");
	}
	else
	{
		document.getElementById(inputId).value="";
		getReply(str,1);
		/*replyContentId="replyContentBlog"+str;
		replyNumId="replyNumBlog"+str;
		numStr = document.getElementById(replyNumId).innerHTML;
		if(numStr=="评论")
		{
			document.getElementById(replyNumId).innerHTML="评论(1)";
		}
		else
		{
			var num=parseInt(numStr.substring(numStr.indexOf('(')+1,numStr.indexOf(')')))+1;
			//alert(num);
			document.getElementById(replyNumId).innerHTML="评论("+num+")";
			
		}
		document.getElementById(replyContentId).innerHTML=ajax.responseText+document.getElementById(replyContentId).innerHTML;
		*/
	}
}
} 
}

function reReply(bid,replyid,username)
{
	var inputId="textReplyBlog"+bid;
	var buttonId="buttonReplyBlog"+bid;
	
	document.getElementById(inputId).value="回复@"+username+"：";
	document.getElementById(buttonId).onclick=function(){
		doReReply(bid,replyid);
		};
	
}

function doReReply(bid,replyid)
{
//获取表单对象和用户信息值
var inputId = "textReplyBlog"+bid;
var content = document.getElementById(inputId).value;

//接收表单的URL地址
var url = "reReply.php";

//需要POST的值，把每个变量都通过&来联接
var postStr= "bid="+bid+"&replyid="+replyid+"&content="+content;

var ajax = false;
//开始初始化XMLHttpRequest对象
if(window.XMLHttpRequest) { //Mozilla 浏览器
ajax = new XMLHttpRequest();
if (ajax.overrideMimeType) {//设置MiME类别
ajax.overrideMimeType("text/xml");
}
}
else if (window.ActiveXObject) { // IE浏览器
try {
ajax = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
ajax = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!ajax) { // 异常，创建对象实例失败
window.alert("不能创建XMLHttpRequest对象实例.");
return false;
}        

//通过Post方式打开连接
ajax.open("POST", url, true);

//定义传输的文件HTTP头信息
ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");

//发送POST数据
ajax.send(postStr);

//获取执行状态
ajax.onreadystatechange = function() {
//如果执行状态成功，那么就把返回信息输出
if (ajax.readyState == 4 && ajax.status == 200) {
	if(ajax.responseText.Trim()=="评论失败")
	{
		alert("评论失败");
	}
	else
	{
		document.getElementById(inputId).value="";
		replyContentId="replyContentBlog"+bid;
		replyNumId="replyNumBlog"+bid;
		numStr = document.getElementById(replyNumId).innerHTML;
		if(numStr=="评论")
		{
			document.getElementById(replyNumId).innerHTML="评论(1)";
		}
		else
		{
			var num=parseInt(numStr.substring(numStr.indexOf('(')+1,numStr.indexOf(')')))+1;
			//alert(num);
			document.getElementById(replyNumId).innerHTML="评论("+num+")";
			
		}
		document.getElementById(replyContentId).innerHTML=ajax.responseText+document.getElementById(replyContentId).innerHTML;
		var buttonId="buttonReplyBlog"+bid;
		document.getElementById(buttonId).onclick=function(){
			reply(bid);
		};
	}
}
} 
}

$(document).ready(function(){
	  $("#blogOK").click(function(){	    
	    blog=$("#blog").val();
	    if(blog=="")
	    {
		    alert('请输入博文内容');
		    return;
		}
	    picName=$("#isPicUp").val();
	    $.post("/tpapp/Xx/postBlog",{isPicUp:picName,blog:blog},function(result){
		  if(result.Trim()=="发布失败")
	      {
		      alert("发布失败");
		  }
	      else
	      {
			  $("#blog").val("");
		      $("#isPicUp").val("");
		      $("#preview").hide();
		      news=$("#divNews").html();
		      if(news.trim()=="暂无新鲜事")
		      {
			      news=result;
		    	  $("#divNews").html(news);
			  }
		      else
		      {
			      news=result+news;
		    	  $("#divNews").html(news);
			  }
		      //alert("发布成功"); 
		  }
	    });
	  });
	});


function delBlog(bid)
{

//接收表单的URL地址
var url = "delBlog.php";

//需要POST的值，把每个变量都通过&来联接
var postStr= "bid="+bid;


var ajax = false;
//开始初始化XMLHttpRequest对象
if(window.XMLHttpRequest) { //Mozilla 浏览器
ajax = new XMLHttpRequest();
if (ajax.overrideMimeType) {//设置MiME类别
ajax.overrideMimeType("text/xml");
}
}
else if (window.ActiveXObject) { // IE浏览器
try {
ajax = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
ajax = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!ajax) { // 异常，创建对象实例失败
window.alert("不能创建XMLHttpRequest对象实例.");
return false;
}        

//通过Post方式打开连接
ajax.open("POST", url, true);

//定义传输的文件HTTP头信息
ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");

//发送POST数据
ajax.send(postStr);

//获取执行状态
ajax.onreadystatechange = function() {
//如果执行状态成功，那么就把返回信息输出
if (ajax.readyState == 4 && ajax.status == 200) {
	if(ajax.responseText.Trim()=="删除失败")
	{
		alert("删除失败！");
	}
	else
	{
		var divBlogAllId="divBlogAll"+bid;
		var hrId = "hr"+bid;
		$("#"+divBlogAllId).hide();
		$("#"+hrId).hide();
		alert("删除成功！");
	}
}
}; 
}

function showReply(obj,str)
{	
	var replyId = "reply"+str;
	document.getElementById(replyId).style.display="";
	obj.onclick=function(){hideReply(this,str)};

	var buttonId="buttonReply"+str;
	var bid = str.substring(4);
	document.getElementById(buttonId).onclick=function(){
		reply(bid);
	};
	
	var inputId = "textReply"+str;
	document.getElementById(inputId).value="";
}

function hideReply(obj,str)
{
	var replyId = "reply"+str;
	document.getElementById(replyId).style.display="none";
	obj.onclick=function(){showReply(this,str)};
}

function toggleReply(obj,str){
	var replyId = "reply"+str;
	$('#'+replyId).toggle();

	var buttonId="buttonReply"+str;
	var bid = str.substring(4);
	$('#'+buttonId).click=function(){
		reply(bid);
	};
	
	var inputId = "textReply"+str;
	$('#'+inputId).val('');
	
	getReply(bid,1);
	
}

function getReply(bid,p){
	$.ajax({
		url:'/tpapp/Xx/getReply?bid='+bid+'&p='+p,
		type:'get',
		//dataType:'json',
		success:function(data,status){
			var html = data.substring(0,data.lastIndexOf("-")-1);
			var count = data.substr(data.lastIndexOf("-")+1);
			$('#replyContentBlog'+bid).html(html);
			$('#replyNumBlog'+bid).html("评论"+((count==0)?"":("("+count+")")));
		},
		error:function(data,status){
			alert(status);
		}
	});
}

$(function() {
	var forwardingContent = $( "#forwardingContent" ),
		allFields = $( [] ).add( forwardingContent ),
		tips = $( ".validateTips" );

	function updateTips( t ) {
		tips
			.text( t )
			.addClass( "ui-state-highlight" );
		setTimeout(function() {
			tips.removeClass( "ui-state-highlight", 1500 );
		}, 500 );
	}

	function checkLength( o, n, min, max ) {
		if ( o.val().length > max || o.val().length < min ) {
			o.addClass( "ui-state-error" );
			updateTips( "转发内容字数必须在 " +
				min + " 和 " + max + "之间." );
			return false;
		} else {
			return true;
		}
	}

	function checkRegexp( o, regexp, n ) {
		if ( !( regexp.test( o.val() ) ) ) {
			o.addClass( "ui-state-error" );
			updateTips( n );
			return false;
		} else {
			return true;
		}
	}

	$( "#dialog-form" ).dialog({
		autoOpen: false,
		height: 300,
		width: 405,
		modal: true,
		buttons: {
			"转发": function() {
				var bValid = true;
				allFields.removeClass( "ui-state-error" );

				bValid = bValid && checkLength( forwardingContent, "forwardingContent", 1, 140 );


				if ( bValid ) {
				    content = document.getElementById("forwardingContent").value;
				    $.post("forwarding.php",{content:content,bid:blogId},function(result){
					  if(result.Trim()=="转发失败")
				      {
					      alert("转发失败");
					  }
				      else
				      {
						  alert("转发成功");
					      news=$("#divNews").html();
					      if(news.trim()=="暂无新鲜事")
					      {
						      news=result;
					    	  $("#divNews").html(news);
						  }
					      else
					      {
						      news=result+news;
					    	  $("#divNews").html(news);
						  }
					  }
				    });
					$( this ).dialog( "close" );
				}
			},
			"取消": function() {
				$( this ).dialog( "close" );
			}
		},
		close: function() {
			allFields.val( "" ).removeClass( "ui-state-error" );
		}
	});

	$( "#create-user" )
		.button()
		.click(function() {
			$( "#dialog-form" ).dialog( "open" );
		});
});

function openLayer(objId,conId,isForwarding){ //打开转发对话框
	
	blogId = objId.substr(14);
	var contentBlogId = "contentBlog"+blogId;
	document.getElementById("originalBlog").innerHTML=document.getElementById(contentBlogId).innerHTML;
	if(isForwarding!="")
	{
		document.getElementById("forwardingContent").value=isForwarding;
	}
	$( "#dialog-form" ).dialog( "open" );
	
}